package cfca.seal.util;

import cfca.seal.exception.CodeException;
import cfca.seal.system.Env;
import cfca.seal.web.vo.AutoSealModelVo;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import javax.imageio.ImageIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OutPicture
{
  private static final Logger log = LoggerFactory.getLogger("web_sys");

  private static BufferedImage bufferedImageUtil(String message, String messageFont, String centerName, String centFont, int centtopdown, int centleftright, String bottomName, String btFont, int bttopdown, int btleftright, String bottomName1, String bt1Font, int bt1topdown, int bt1leftright, int width, int height, int type, String topFontSize, float spacingRatio)
    throws FileNotFoundException, IOException
  {
    String path = null;
    if (type == 0)
      path = Env.sealResourceDir + File.separator + "starmodel.png";
    else {
      path = Env.sealResourceDir + File.separator + "model.png";
    }
    BufferedImage image = ImageIO.read(new FileInputStream(path));

    if (StringUtil.isNotEmpty(message)) {
      int CENTERX = 260;
      int CENTERY = 260;
      int radius = 230;
      Graphics2D g2 = image.createGraphics();
      g2.setColor(Color.RED);

      g2.setFont(new Font(centFont, 1, 40));
      if (centerName != null) {
        g2.drawString(centerName, width + centleftright, height + centtopdown);
      }
      g2.setFont(new Font(btFont, 1, 20));
      if (bottomName != null) {
        g2.drawString(bottomName, width + 35 + btleftright, height + 20 + bttopdown);
      }
      g2.setFont(new Font(bt1Font, 1, 15));
      if (bottomName1 != null) {
        g2.drawString(bottomName1, width + 45 + bt1leftright, height + 35 + bt1topdown);
      }

      String[] messages2 = message.split("", 0);
      String[] messages = new String[messages2.length - 1];
      System.arraycopy(messages2, 1, messages, 0, messages2.length - 1);

      int ilength = messages.length;

      int fontsize = Integer.valueOf(topFontSize).intValue();
      Font f = new Font(messageFont, 1, fontsize);
      FontRenderContext context = g2.getFontRenderContext();
      Rectangle2D bounds = f.getStringBounds(message, context);

      double char_interval = bounds.getWidth() / ilength;

      double ascent = -bounds.getY();
      int first = 0; int second = 0;
      boolean odd = false;
      if (ilength % 2 == 1) {
        first = (ilength - 1) / 2;
        odd = true;
      } else {
        first = ilength / 2 - 1;
        second = ilength / 2;
        odd = false;
      }
      double radius2 = 230.0D - ascent;
      double x0 = 260.0D;
      double y0 = 30.0D + ascent;

      double a = spacingRatio * Math.asin(char_interval / (2.0D * radius2));
      System.out.println(a);
      if (odd) {
        g2.setFont(f);
        g2.drawString(messages[first], (float)(x0 - char_interval / 2.0D), (float)y0);

        for (int i = first + 1; i < ilength; i++) {
          double aa = (i - first) * a;
          double ax = radius2 * Math.sin(aa);
          double ay = radius2 - radius2 * Math.cos(aa);
          AffineTransform transform = AffineTransform.getRotateInstance(aa);
          Font f2 = f.deriveFont(transform);
          g2.setFont(f2);
          g2.drawString(messages[i], (float)(x0 + ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 + ay - char_interval / 2.0D * Math.sin(aa)));
        }

        for (int i = first - 1; i > -1; i--) {
          double aa = (first - i) * a;
          double ax = radius2 * Math.sin(aa);
          double ay = radius2 - radius2 * Math.cos(aa);
          AffineTransform transform = AffineTransform.getRotateInstance(-aa);
          Font f2 = f.deriveFont(transform);
          g2.setFont(f2);
          g2.drawString(messages[i], (float)(x0 - ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 + ay + char_interval / 2.0D * Math.sin(aa)));
        }
      }
      else {
        for (int i = second; i < ilength; i++) {
          double aa = (i - second + 0.5D) * a;
          double ax = radius2 * Math.sin(aa);
          double ay = radius2 - radius2 * Math.cos(aa);
          AffineTransform transform = AffineTransform.getRotateInstance(aa);
          Font f2 = f.deriveFont(transform);
          g2.setFont(f2);
          g2.drawString(messages[i], (float)(x0 + ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 + ay - char_interval / 2.0D * Math.sin(aa)));
        }

        for (int i = first; i > -1; i--) {
          double aa = (first - i + 0.5D) * a;
          double ax = radius2 * Math.sin(aa);
          double ay = radius2 - radius2 * Math.cos(aa);
          AffineTransform transform = AffineTransform.getRotateInstance(-aa);
          Font f2 = f.deriveFont(transform);
          g2.setFont(f2);
          g2.drawString(messages[i], (float)(x0 - ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 + ay + char_interval / 2.0D * Math.sin(aa)));
        }
      }
      g2.dispose();
    }
    return image;
  }

  public static byte[] getCirclePngToByte(AutoSealModelVo sealModelVo)
  {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BufferedImage buffImg = null;
    try
    {
      buffImg = bufferedImageUtil(sealModelVo.getTop(), sealModelVo.getTopFontFamily(), sealModelVo.getBottom(), sealModelVo.getBottomFontFamily(), sealModelVo.getBottomUpDown(), sealModelVo.getBottomLeftRight(), sealModelVo.getRider1(), sealModelVo.getRider1FontFamily(), sealModelVo.getRider1UpDown(), sealModelVo.getRider1LeftRight(), sealModelVo.getRider2(), sealModelVo.getRider2FontFamily(), sealModelVo.getRider2UpDown(), sealModelVo.getRider2LeftRight(), 180, 380, sealModelVo.getFiveStar() == null ? 1 : 0, sealModelVo.getTopFontSize(), sealModelVo.getSpacingRatio());

      ImageIO.write(buffImg, "png", byteArrayOutputStream);
    }
    catch (IOException e) {
      e.printStackTrace();
      log.error("生成圆形图片出错", e);
    }
    return ImageUtil.resizeImage(byteArrayOutputStream.toByteArray(), sealModelVo.getImageWidth(), sealModelVo.getImageHeight());
  }

  public static byte[] formatImageAddBusinessCode(byte[] imageData, int imageWidth, int imageHeight, String businessCode, float fontSize, float ratioX, float ratioY, Color color)
  {
    BufferedImage image = null;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
      int width = imageWidth;
      int height = (int)(ratioY * imageHeight);

      int size = (int)(fontSize * width / 100.0F);

      width = (int)(ratioX * width);
      ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
      image = ImageIO.read(bais);

      Graphics2D g2 = image.createGraphics();
      g2.setColor(color);
      g2.setFont(new Font("Times", 1, size));
      g2.drawString(businessCode, width, height);
      g2.dispose();

      ImageIO.write(image, "png", byteArrayOutputStream);
    } catch (IOException e) {
      e.printStackTrace();
    }

    return ImageUtil.resizeImage(byteArrayOutputStream.toByteArray(), imageWidth, imageHeight);
  }

  private static byte[] getSquarePng(String message, int width, int height, String fontFamily, int fontSize, int type, String fontColor)
    throws CodeException
  {
    BufferedImage buffImg = null;
    if (message != null) {
      buffImg = new BufferedImage(fontSize * 2, fontSize * 2, 1);
      Graphics2D g2 = buffImg.createGraphics();

      buffImg = g2.getDeviceConfiguration().createCompatibleImage(fontSize * 2, fontSize * 2, 3);
      g2 = buffImg.createGraphics();

      Color color = Color.RED;
      try {
        int rColor = Integer.parseInt(fontColor.substring(0, 2), 16);
        int gColor = Integer.parseInt(fontColor.substring(2, 4), 16);
        int bColor = Integer.parseInt(fontColor.substring(4, 6), 16);
        color = new Color(rColor, gColor, bColor);
      } catch (Exception e) {
        throw new CodeException("C2009", "输入的字体颜色不正确");
      }
      g2.setColor(color);

      g2.setFont(new Font(fontFamily, 0, fontSize));
      String newMessage = "";
      if (message.length() == 1)
        newMessage = message + "之印章";
      else if (message.length() == 2)
        newMessage = message + "之章";
      else if (message.length() == 3)
        newMessage = message + "章";
      else {
        newMessage = message.substring(0, 4);
      }

      char[] charArray = newMessage.toCharArray();
      if (type == 0)
      {
        g2.drawString(new String(new char[] { charArray[0] }), fontSize, fontSize - fontSize / 8);
        g2.drawString(new String(new char[] { charArray[1] }), fontSize, fontSize * 2 - fontSize / 8);
        g2.drawString(new String(new char[] { charArray[2] }), 0, fontSize - fontSize / 8);
        g2.drawString(new String(new char[] { charArray[3] }), 0, fontSize * 2 - fontSize / 8);
      }
      else {
        g2.drawString(new String(new char[] { charArray[1] }), fontSize, fontSize - fontSize / 8);
        g2.drawString(new String(new char[] { charArray[3] }), fontSize, fontSize * 2 - fontSize / 8);
        g2.drawString(new String(new char[] { charArray[0] }), 0, fontSize - fontSize / 8);
        g2.drawString(new String(new char[] { charArray[2] }), 0, fontSize * 2 - fontSize / 8);
      }
    }
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
      ImageIO.write(buffImg, "png", byteArrayOutputStream);
    } catch (IOException e) {
      e.printStackTrace();
      log.error("生成方形图片出错", e);
    }

    return ImageUtil.resizeImage(byteArrayOutputStream.toByteArray(), width, height);
  }

  public static byte[] getSquarePngToByte(AutoSealModelVo sealModelVo)
    throws CodeException
  {
    return getSquarePng(sealModelVo.getSquareName(), sealModelVo.getImageWidth(), sealModelVo.getImageHeight(), sealModelVo.getSquareFontFamily(), sealModelVo.getFontSize(), sealModelVo.getSquareType(), sealModelVo.getFontColor());
  }

  public static byte[] getNamePngToByte(String name, int hight, int width, int fontSize, String fontColor)
    throws CodeException
  {
    return getSquarePng(name, width, hight, "宋体", fontSize, 0, fontColor);
  }

  public static byte[] getRectanglePng(String message, int width, int height, int fontSize, String fontColor, String fontFamily)
    throws CodeException
  {
    BufferedImage buffImg = null;
    if (message != null) {
      String[] msg = message.split("##");
      int maxLength = msg[0].length();
      for (int i = 0; i < msg.length; i++) {
        if (msg[i].length() > maxLength) {
          maxLength = msg[i].length();
        }
      }
      buffImg = new BufferedImage(maxLength * fontSize + maxLength * 2, msg.length * fontSize, 1);
      Graphics2D g2 = buffImg.createGraphics();
      buffImg = g2.getDeviceConfiguration().createCompatibleImage(maxLength * fontSize + maxLength * 2, msg.length * fontSize, 3);
      g2 = buffImg.createGraphics();
      Color color = Color.RED;
      try {
        int rColor = Integer.parseInt(fontColor.substring(0, 2), 16);
        int gColor = Integer.parseInt(fontColor.substring(2, 4), 16);
        int bColor = Integer.parseInt(fontColor.substring(4, 6), 16);
        color = new Color(rColor, gColor, bColor);
      } catch (Exception e) {
        throw new CodeException("C2009", "输入的字体颜色不正确");
      }
      g2.setColor(color);
      g2.setFont(new Font(fontFamily, 0, fontSize));
      for (int j = 0; j < msg.length; j++) {
        char[] charArray = msg[j].toCharArray();
        long lengthPerChar = maxLength * fontSize / msg[j].length();
        for (int i = 0; i < msg[j].length(); i++) {
          g2.drawString(new String(new char[] { charArray[i] }), (float)(2 + fontSize * i + i * (lengthPerChar - fontSize) * 3L / 2L), fontSize - fontSize / 8 + fontSize * j);
        }
      }
    }

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
      ImageIO.write(buffImg, "png", byteArrayOutputStream);
    } catch (IOException e) {
      e.printStackTrace();
    }

    return ImageUtil.resizeImage(byteArrayOutputStream.toByteArray(), width, height);
  }

  public static byte[] getRectanglePngToByte(AutoSealModelVo sealModelVo)
    throws CodeException
  {
    return getRectanglePng(sealModelVo.getSquareName(), sealModelVo.getImageWidth(), sealModelVo.getImageHeight(), sealModelVo.getFontSize(), sealModelVo.getFontColor(), sealModelVo.getSquareFontFamily());
  }

  public static byte[] getRectanglePng(String message, int width, int height, int fontSize, String fontColor)
    throws CodeException
  {
    return getRectanglePng(message, width, height, fontSize, fontColor, "宋体");
  }

  public static byte[] getCirclePngToByte(String imageName, String imageName2, int height, int width) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BufferedImage buffImg = null;
    try
    {
      int CENTERX = 260;
      int CENTERY = 260;
      int radius = 230;

      String path = Env.sealResourceDir + File.separator + "starmodel.png";
      buffImg = ImageIO.read(new FileInputStream(path));
      Graphics2D g2 = buffImg.createGraphics();
      g2.setColor(Color.RED);

      if (imageName2 != null) {
        Font font = new Font("宋体", 1, 40);
        g2.setFont(font);
        FontRenderContext context = g2.getFontRenderContext();
        Rectangle2D bounds = font.getStringBounds(imageName2, context);
        int length = imageName2.length();
        bounds = font.getStringBounds(imageName2, context);
        int char_interval = (int)(bounds.getWidth() / length);
        g2.drawString(imageName2, 260 - char_interval * length / 2, 400);
      }

      String[] messages2 = imageName.split("", 0);
      String[] messages = new String[messages2.length - 1];
      System.arraycopy(messages2, 1, messages, 0, messages2.length - 1);

      int ilength = messages.length;
      Font f = new Font("宋体", 1, 60);
      FontRenderContext context = g2.getFontRenderContext();
      Rectangle2D bounds = f.getStringBounds(imageName, context);

      double char_interval = bounds.getWidth() / ilength;

      double ascent = -bounds.getY();
      int first = 0; int second = 0;
      boolean odd = false;
      if (ilength % 2 == 1) {
        first = (ilength - 1) / 2;
        odd = true;
      } else {
        first = ilength / 2 - 1;
        second = ilength / 2;
        odd = false;
      }
      double radius2 = 230.0D - ascent;
      double x0 = 260.0D;
      double y0 = 30.0D + ascent;

      double a = 2.0D * Math.asin(char_interval / (2.0D * radius2));
      if (odd) {
        g2.setFont(f);
        g2.drawString(messages[first], (float)(x0 - char_interval / 2.0D), (float)y0);

        for (int i = first + 1; i < ilength; i++) {
          double aa = (i - first) * a;
          double ax = radius2 * Math.sin(aa);
          double ay = radius2 - radius2 * Math.cos(aa);
          AffineTransform transform = AffineTransform.getRotateInstance(aa);
          Font f2 = f.deriveFont(transform);
          g2.setFont(f2);
          g2.drawString(messages[i], (float)(x0 + ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 + ay - char_interval / 2.0D * Math.sin(aa)));
        }

        for (int i = first - 1; i > -1; i--) {
          double aa = (first - i) * a;
          double ax = radius2 * Math.sin(aa);
          double ay = radius2 - radius2 * Math.cos(aa);
          AffineTransform transform = AffineTransform.getRotateInstance(-aa);
          Font f2 = f.deriveFont(transform);
          g2.setFont(f2);
          g2.drawString(messages[i], (float)(x0 - ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 + ay + char_interval / 2.0D * Math.sin(aa)));
        }
      }
      else {
        for (int i = second; i < ilength; i++) {
          double aa = (i - second + 0.5D) * a;
          double ax = radius2 * Math.sin(aa);
          double ay = radius2 - radius2 * Math.cos(aa);
          AffineTransform transform = AffineTransform.getRotateInstance(aa);
          Font f2 = f.deriveFont(transform);
          g2.setFont(f2);
          g2.drawString(messages[i], (float)(x0 + ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 + ay - char_interval / 2.0D * Math.sin(aa)));
        }

        for (int i = first; i > -1; i--) {
          double aa = (first - i + 0.5D) * a;
          double ax = radius2 * Math.sin(aa);
          double ay = radius2 - radius2 * Math.cos(aa);
          AffineTransform transform = AffineTransform.getRotateInstance(-aa);
          Font f2 = f.deriveFont(transform);
          g2.setFont(f2);
          g2.drawString(messages[i], (float)(x0 - ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 + ay + char_interval / 2.0D * Math.sin(aa)));
        }
      }
      g2.dispose();
      ImageIO.write(buffImg, "png", byteArrayOutputStream);
    } catch (IOException e) {
      e.printStackTrace();
      log.error("", e);
    }
    return ImageUtil.resizeImage(byteArrayOutputStream.toByteArray(), width, height);
  }
}